﻿// Deep Earth is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warranty – Use at your own risk
// View the project and the latest code at http://DeepEarth.codeplex.com/

using System.Windows;
using System.Windows.Media;
using GeoAPI.Geometries;

namespace DeepEarth.Client.MapControl.Geometries
{
    [TemplatePart(Name = PART_CompositeTransform, Type = typeof(CompositeTransform))]
    public class PointControl : GeometryControlBase<IGeometry>
    {
        private const string PART_CompositeTransform = "PART_CompositeTransform";
        private CompositeTransform _compositeTransform;

        private Point _logicalCoordinate;

        /// <summary>
        /// The System.Windows.Point class
        /// </summary>
        public Location Location
        {
            get
            {
                return new Location
                {
                    Longitude = GeometryItem.Coordinate.X,
                    Latitude = GeometryItem.Coordinate.Y,
                    Altitude = GeometryItem.Coordinate.Z
                };
            }
            set
            {
                GeometryItem.Coordinate.X = value.Longitude;
                GeometryItem.Coordinate.Y = value.Latitude;
                GeometryItem.Coordinate.Z = value.Altitude;

                var location = new Location
                               {
                                   Longitude = value.Longitude,
                                   Latitude = value.Latitude,
                                   Altitude = value.Altitude
                               };

                _logicalCoordinate = Map.DefaultInstance.CoordHelper.GeoToLogical(location);
                Refresh();
            }
        }

        public PointControl()
            : base(new GisSharpBlog.NetTopologySuite.Geometries.Point(double.NaN, double.NaN))
        {
            DefaultStyleKey = typeof(PointControl);
            _logicalCoordinate = new Point();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _compositeTransform = (CompositeTransform)GetTemplateChild(PART_CompositeTransform);
            _compositeTransform.CenterX = Anchor.X;
            _compositeTransform.CenterY = Anchor.Y;

            IsLoaded = true;
            ForceMeasure();

            if (Layer != null)
            {
                Layer.UpdateChildLocation(this);
            }
        }

        /// <summary>
        /// The logical coordinate of the location
        /// </summary>
        public Point LogicalCoordinate
        {
            get { return _logicalCoordinate; }
        }

        internal override void UpdateLocation()
        {
            Refresh();
        }

        /// <summary>
        /// The adjustment scale for the Point
        /// </summary>
        public override double ScaleAdjustment
        {
            get { return base.ScaleAdjustment; }
            set
            {
                base.ScaleAdjustment = value;
                //if (_isLoaded)
                if (_compositeTransform != null)
                {
                    _compositeTransform.ScaleX = ScaleAdjustment;
                    _compositeTransform.ScaleY = ScaleAdjustment;
                }
            }
        }

        public override bool Intersects(Rect bounds)
        {
            // TODO: Implement this Method
            //var point = new Point(Point.X, Point.Y);
            //return bounds.Contains(point);

            //var x = bounds.Contains(_point);
            // return x;

            return true;
        }
    }
}